#!/usr/bin/python

# a simple script to cut the dataset name
# When useing dq2-ls to get the dataset list, it will return the full dataset name, with tid at the end,
# but when submitting grid jobs, we do not need the tid, so the dataset name should be truncated
# only to keep till tag, like p1035
# This script will also throw away the overlapped dataset, only keep the latest one
# Lailin, Sep. 2012, Ann Arbor

import sys

def cut_dataset_name( datalist='data11_p1035_Muons.list', output='data11_NTUP_SMWZ_p1035_Muons.list', **kw):

    print 'INFO ==> Input dataset list: ', datalist
    print 'INFO ==> Output dataset list: ', output
    #list='data11_p1035_Muons.list'
    #output='data11_NTUP_SMWZ_p1035_Muons.list'
    try: flist=open(datalist,'r')
    except IOError:
        print 'ERROR ==> This file can not be opened %s !!!' % list
        sys.exit(1)
    try: fout=open(output,'w')
    except IOError:
        print 'ERROR ==> This file can not be opened %s !!!' % output
        sys.exit(1)
    newlist=[]
    newlist0=[]
    list_info={}
    for this_line in flist.readlines():
        # Drop the CR if there is one at the end of this line
        if this_line[-1]=='\n': line=this_line[:-1]
        else : line=this_line
        # Split the line
        sline=line.split('_tid')
        # Skip empty line
        if len(sline)==0 : continue
        # remove overlaped dataset
        ssline=sline[0].split('NTUP_SMWZ')
        if not ssline[0] in newlist0: 
            newlist0.append(ssline[0])
            list_info[ssline[0]]=sline[0]
        else:
            if sline[0] > list_info[ssline[0]]: 
                list_info[ssline[0]]=sline[0]
    newlist0.sort()
    for list in newlist0:
        fout.write("%s/\n" % list_info[list])
    flist.close()
    fout.close()
    print 'Done!'

###################
## Main Function ##
###################
if __name__ == "__main__":
    cut_dataset_name(sys.argv[1], sys.argv[2])
